/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dipl.algorithm.math.fp.utility;

import dipl.algorithm.math.fp.primitive.Point2df;
import java.util.Random;

/**
 * provides static methods for creating Point2d-sets
 */
public class Point2dSetFactory {

	/**
	 * parses given string containing control point coordinates.
	 * @param s
	 * @return curve or <code>null</code> if parsing failed
	 */
	public static Point2df[] CreateFromString( String s ) {
		s = s.trim();
		if( !s.startsWith( "{" ) || !s.endsWith( "}" ) ) {
			System.err.println( "Wrong syntax: "+s+"!" );
			return null;
		}
		s = s.substring( 1, s.length()-1 );
		// extract points
		String[] spoints = s.split( ";" );
		Point2df[] points = new Point2df[spoints.length];
		for( int i = 0; i < spoints.length; i++ ) {
			String t = spoints[i];
			t = t.trim();
			if( t.length() == 0 ) {
				System.err.println( "Wrong syntax in: "+s+"!" );
				return null;
			}
			if( !t.startsWith( "(" ) || !t.endsWith( ")" ) ) {
				System.err.println( "Wrong syntax in: "+s+"!" );
				return null;
			}
			t = t.substring( 1, t.length()-1 );
			t = t.trim();
			if( t.length() == 0 ) {
				System.err.println( "Wrong syntax in: "+s+"!" );
				return null;
			}
			String[] coords = t.split( "," );
			if( coords.length != 2 ) {
				System.err.println( "Wrong syntax in: "+s+"!" );
				return null;
			}
			points[i] = new Point2df( Double.valueOf( coords[0] ), Double.valueOf( coords[1] ) );
		}
		if( points.length == 0 ) {
			System.err.println( "No points in:"+s+"!" );
			return null;
		}
		return points;
	}

	/**
	 * Creates an array of random Point2d's using precision given by FPMath-object.
	 * param rand random number generator
	 * @param L number of bits maximally used in mantissa of resulting points, must be greater zero!
	 * param numPoints number of points to generate (must be greater 0!)
	 * @return point-array
	 */
	public static Point2df[] CreateRandom( Random rand, int numPoints ) {
		Point2df[] res = new Point2df[numPoints];
		for( int i = 0; i < res.length; i++ ) {
			res[i] = new Point2df( -1.0+2.0*rand.nextDouble(), -1.0+2.0*rand.nextDouble() );
		}
		return res;
	}
}
